package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class RegDemo {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner scan = new Scanner(System.in);
        System.out.println("欢迎注册,请输入用户名:");
        String username = scan.nextLine();
        System.out.println("请输入密码:");
        String password = scan.nextLine();
        System.out.println("请输入昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入年龄:");
        int age = scan.nextInt();

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                //数据库地址格式:jdbc:mysql://数据库IP:端口/数据库名?一堆参数
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",//数据库用户名
                "root"//数据库密码
        );
        Statement statement = connection.createStatement();

        String sql = "INSERT INTO userinfo(username,password,nickname,age) "+"VALUES('"+username+"','"+password+"','"+nickname+"',"+age+")";
        System.out.println(sql);

        int num = statement.executeUpdate(sql);
        if(num>0){
            System.out.println("插入完毕!");
        }
        connection.close();
    }
}
